// pages/registered/registered.js
Page({

  data: {
    phoneNumber: null,
    pwd: null,
    confirmPwd: null,
    userName:null
  },

  getPhoneNumber: function (e) {
    this.setData({
      phoneNumber: e.detail.value
    })
  },
  getUserName:function(e){
    this.setData({
      userName: e.detail.value
    })
  },

  getPwd: function (e) {
    this.setData({
      pwd: e.detail.value
    })
  },

  confirmPwd:function(e){
    this.setData({
      confirmPwd: e.detail.value
    })
  },

  registeredButton:function(){
    if (this.data.phoneNumber == null) {
      wx.showToast({
        title: '请输入手机号',
        image: '../../img/login/error.png',
        duration: 2000
      })
      return false;
    }
    if (this.data.phoneNumber.length != 11) {
      wx.showToast({
        title: '手机号长度有误',
        image: '../../img/login/error.png',
        duration: 2000
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(this.data.phoneNumber)) {
      wx.showToast({
        title: '手机号无效',
        image: '../../img/login/error.png',
        duration: 2000
      })
      return false;
    }
    if (this.data.userName == null) {
      wx.showToast({
        title: '请输入用户名',
        image: '../../img/login/error.png',
        duration: 2000
      })
      return false;
    }
    if (this.data.pwd == null){
      wx.showToast({
        title: '请输入密码',
        image: '../../img/login/error.png',
        duration: 2000
      })
      return false;
    }
    if (this.data.pwd != this.data.confirmPwd) {
      wx.showToast({
        title: '密码不一致',
        image: '../../img/login/error.png',
        duration: 2000
      })
      return false;
    }
    var that = this;
    wx.request({
      url: 'http://localhost:8081/registered/'+that.data.phoneNumber+'/'+that.data.userName+'/'+that.data.pwd,
      method: "POST",
      success(res){
        if(res.data=="success"){
          wx.showModal({
            title: '注册成功',
            content: '返回登录',
            success(res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }else{
          wx.showToast({
            title: res.data,
            image: '../../img/login/error.png',
            duration: 2000
          })
        }
      }
    })

    
  },

  goBack: function(){
    wx.navigateBack({
      delta: 1
    })
  }

})